home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-04 / smalltlk.zip / PRELUDE / SCOLLECT.ST < prev    next >
Text File  |  1987-06-17  |  5KB  |  133 lines

  1. Class SequenceableCollection :KeyedCollection
  2. [
  3.     , aCollection
  4.         ^ self coerce: (List new ;
  5.                     addAllLast: self ;
  6.                     addAllLast: aCollection )
  7. |
  8.         collect: aBlock
  9.         ^ self coerce:
  10.              (self inject: List new
  11.                            into: [:x :y | x addLast: (aBlock value: y) . x ] )
  12. |
  13.     copyFrom: start to: stop                | newcol |
  14.                 newcol <- List new.
  15.         (start to: stop) do: [:i | newcol addLast: (self at: i)].
  16.                 ^ self coerce: newcol
  17. |
  18.     copyWith: newElement
  19.         ^ self coerce: (List new ;
  20.                     addAllLast: self ;
  21.                     addLast: newElement )
  22. |
  23.     copyWithout: oldElement                 | newcol |
  24.                 newcol <- List new.
  25.                 self do: [ :x | (x == oldElement)
  26.                                 ifFalse: [ newcol addLast: x ]].
  27.                 ^ self coerce: newcol
  28. |
  29.     equals: aSubCollection startingAt: anIndex      | i |
  30.                 i <- 0.
  31.                 self do: [:x |
  32.                         (x = (aSubCollection at: (anIndex + i)
  33.                                             ifAbsent: [^ false]))
  34.                                 ifFalse: [^ false].
  35.                         i <- i + 1].
  36.                 ^ true
  37. |
  38.     findFirst: aBlock
  39.         ^ self findFirst: aBlock
  40.             ifAbsent: [self error: 'first element not found']
  41. |
  42.     findFirst: aBlock ifAbsent: exceptionBlock
  43.                 self do: [:x | (aBlock value: x)
  44.                 ifTrue: [ ^ self currentKey]].
  45.                 ^ exceptionBlock value
  46. |
  47.     findLast: aBlock
  48.         self findLast: aBlock
  49.             ifAbsent: [self error: 'last element not found']
  50. |
  51.     findLast: aBlock ifAbsent: exceptionBlock
  52.                 self reverseDo: [:x | (aBlock value: x)
  53.                                         ifTrue: [ ^ self currentKey]].
  54.                 ^ exceptionBlock value
  55. |
  56.     indexOfSubCollection: aSubCollection
  57.     startingAt: anIndex
  58.     ifAbsent: exceptionBlock              | n m |
  59.  
  60.                 n <- anIndex.
  61.                 m <- self size - aSubCollection size.
  62.                 [n <= m] whileTrue:
  63.                         [(aSubCollection equals: self startingAt: n)
  64.                                 ifTrue: [^ n].
  65.                          n <- n + 1].
  66.                 ^ exceptionBlock value
  67. |
  68.     indexOfSubCollection: aSubCollection startingAt: anIndex
  69.                 ^ self indexOfSubCollection: aSubCollection
  70.                startingAt: anIndex
  71.                        ifAbsent: [ self error: 'element not found'. nil]
  72. |
  73.     last
  74.                 ^ (0 = self size) ifFalse: [ self at: self lastKey ]
  75. |
  76.     replaceFrom: start to: stop with: repcol
  77.         repcol inject: start
  78.                into: [:x :y | self at: x put: y. x + 1]
  79. |
  80.     replaceFrom: first to: stop with: repcol startingAt: repStart | i |
  81.                 i <- 0 .
  82.                 [(first + i) <= stop] whileTrue:
  83.                         [self at: (first + i)
  84.                               put: (repcol at: i + repStart).
  85.              i <- i + 1 ]
  86. |
  87.         reverseDo: aBlock                       | n m |
  88.                 n <- self lastKey.  m <- self firstKey.
  89.                 [n >= m] whileTrue:
  90.                         [(self includesKey: n) ifTrue:
  91.                                 [aBlock value: (self at: n)].
  92.                          n <- n - 1].
  93.                 ^ nil
  94. |
  95.     reversed                | newar i |
  96.                 newar <- Array new: (i <- self size).
  97.                 self do: [:x | newar at: i put: x. i <- i - 1].
  98.                 ^ self coerce: newar
  99. |
  100.     select: aBlock
  101.         ^ self coerce:
  102.              (self inject: List new
  103.                            into: [:x :y | (aBlock value: y)
  104.                                            ifTrue: [x addLast: y]. x ] )
  105. |
  106.     sort
  107.         ^ self sort: [:x :y | x <= y]
  108. |
  109.     sort: sortBlock        | index temp newArray |
  110.         newArray <- self asArray.
  111.         (2 to: newArray size) do:
  112.           [ :highIndex | index <- highIndex - 1.
  113.             [(index >= 1) and:
  114.                [(sortBlock value: (newArray at: index)
  115.                       value: (newArray at: (index + 1))) not]]
  116.                whileTrue: [temp <- newArray at: index.
  117.                        newArray at: index
  118.                         put: (newArray at: index + 1).
  119.                        newArray at: index + 1 put: temp.
  120.                        index <- index - 1 ]].
  121.         ^ self coerce: newArray
  122.  
  123. |
  124.     with: aSequenceableCollection do: aBlock        | arg1 arg2 |
  125.                 arg1 <- self first. arg2 <- aSequenceableCollection first.
  126.                 [ arg1 notNil] whileTrue:
  127.                         [ aBlock value: arg1 value: arg2.
  128.                           arg1 <- self next.
  129.                           arg2 <- aSequenceableCollection next].
  130.                 ^ nil
  131.  
  132. ]
  133.